/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * passwordChange.java
 *
 * Created on 19-ene-2012, 20:10:14
 */
package newgaleria;

import java.security.NoSuchAlgorithmException;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import org.jdesktop.application.Action;

/**
 *
 * @author eltorre
 */
public class passwordChange extends javax.swing.JDialog {

    /** Creates new form passwordChange */
    public passwordChange(JTextField externPassword) {
        super();
        initComponents();
        this.externPassword = externPassword;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cambiarButton = new javax.swing.JButton();
        cancelarBoton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        newPassword = new javax.swing.JPasswordField();
        newPasswordConfirm = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();
        currentPassword = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(newgaleria.GaleriaApp.class).getContext().getActionMap(passwordChange.class, this);
        cambiarButton.setAction(actionMap.get("cambiar")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(newgaleria.GaleriaApp.class).getContext().getResourceMap(passwordChange.class);
        cambiarButton.setText(resourceMap.getString("cambiarButton.text")); // NOI18N
        cambiarButton.setName("cambiarButton"); // NOI18N

        cancelarBoton.setText(resourceMap.getString("cancelarBoton.text")); // NOI18N
        cancelarBoton.setName("cancelarBoton"); // NOI18N
        cancelarBoton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarBotonActionPerformed(evt);
            }
        });

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        newPassword.setText(resourceMap.getString("newPassword.text")); // NOI18N
        newPassword.setName("newPassword"); // NOI18N

        newPasswordConfirm.setText(resourceMap.getString("newPasswordConfirm.text")); // NOI18N
        newPasswordConfirm.setName("newPasswordConfirm"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        currentPassword.setText(resourceMap.getString("currentPassword.text")); // NOI18N
        currentPassword.setName("currentPassword"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cancelarBoton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cambiarButton))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(newPasswordConfirm, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(newPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(38, 38, 38)
                        .addComponent(currentPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(currentPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(newPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(newPasswordConfirm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelarBoton)
                    .addComponent(cambiarButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelarBotonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarBotonActionPerformed
        this.dispose();
    }//GEN-LAST:event_cancelarBotonActionPerformed

   
    @Action
    public void cambiar() throws NoSuchAlgorithmException {
        String realAntiguo = sorUtils.CipherSha1.EncriptarSHA1(String.valueOf(currentPassword.getPassword()).getBytes());
        if (realAntiguo.equals(externPassword.getText()))
        {
            String nuevoPass = sorUtils.CipherSha1.EncriptarSHA1(String.valueOf(newPassword.getPassword()).getBytes());
            if (nuevoPass.equals(sorUtils.CipherSha1.EncriptarSHA1(String.valueOf(newPassword.getPassword()).getBytes())))
            {
                externPassword.setText(nuevoPass);
            }
            else
            {
                JOptionPane.showMessageDialog(new JDialog(), 
                    "Error",
                    "El password no coincide con su confirmación",
                    JOptionPane.ERROR_MESSAGE);
            }
        } 
        else
        {
           JOptionPane.showMessageDialog(new JDialog(), 
            "Error",
            "El password antiguo no es correcto",
            JOptionPane.ERROR_MESSAGE);         
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cambiarButton;
    private javax.swing.JButton cancelarBoton;
    private javax.swing.JPasswordField currentPassword;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField newPassword;
    private javax.swing.JPasswordField newPasswordConfirm;
    // End of variables declaration//GEN-END:variables

    JTextField externPassword;
}
